package com.twang.stock.utils.date;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

        /**
         * 判断当前时间是否在 区间内 （精确到毫秒值）：方式：[开,闭)
         * @param sTime	开始区间	HH:mm:ss
         * @param eTime	结束区间	HH:mm:ss
         * @return
         * @author liren
         */
        public static boolean JudgeTime(String sTime, String eTime) {
            long now = System.currentTimeMillis();
            String shortNow = "";
            String judge = "";
            boolean flag = false;
            try {
                // 当前时间 -> yyyy-MM-dd
                String dateShort = DateUtils.getDateShort(now);
                String sDate = String.valueOf(DateUtils.StrToLong(dateShort + " " + sTime));
                String eDate = String.valueOf(DateUtils.StrToLong(dateShort + " " + eTime));
                shortNow = String.valueOf(now);
                judge = "[" + sDate + "," + eDate + "]";

                // IntervalUtil.isInTheInterval工具类，详情请见：https://blog.csdn.net/qq_26465035/article/details/98626346
                flag = IntervalUtil.isInTheInterval(shortNow, judge);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return flag;
        }

        /**
         * 获取当前时间的年月日
         * @param ctime
         * @return
         * @author liren
         */
        public static String getDateShort(long ctime){
            if(ctime<=0){
                return "";
            }
            SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
            return format.format(new Date(ctime)).toString();
        }


        /**
         * 字符串 转时间戳 : yyyy-MM-dd HH:mm:ss --> 时间戳
         * @return
         * @throws ParseException
         * @author liren
         */
        public static long StrToLong(String str) {
            Date date = null;
            long ts = 0;
            if (StringUtils.isNotEmpty(str)) {
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    date = df.parse(str);
                    ts = date.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            return ts;
        }


    /**
     *
     * @param dateShort yyyy-MM-dd
     * @return date
     */
    public  static  Date getDate(String dateShort ) {
        Date date1 = new Date();
        try {
            // 当前时间 -> yyyy-MM-dd
            String date = dateShort + " " + "15:00:00";
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date1=df.parse(date);
        } catch (Exception e) {
        }
        return date1;
    }

    public  static  String getNow() {
        try {
            // 当前时间 -> yyyy-MM-dd
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           return df.format(new Date());
        } catch (Exception e) {
        }
        return null;
    }

    public  static  String gettradeTime() {
        try {
            // 当前时间 -> yyyy-MM-dd
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            return df.format(new Date());
        } catch (Exception e) {
        }
        return null;
    }




    public static void main(String[] args) {
        if(!(DateUtils.JudgeTime("06:17:00","06:50:00")||DateUtils.JudgeTime("13:00:00","15:00:00"))){
            System.out.println("----false----");
        }else{
            System.out.println("----True----");

        }
    }
}
